#include "SettingsDialog.h"
#include "ElaTheme.h"
#include "ElaApplication.h"
#include <QPushButton>
#include <QFormLayout>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QGroupBox>
#include <QSettings>
#include <QApplication>
#include <QMessageBox>

SettingsDialog::SettingsDialog(QWidget* parent) : ElaWidget(parent)
{
    setWindowTitle("设置");
    resize(400, 350);

    // 对话框通常不需要所有按钮
    setWindowButtonFlags(ElaAppBarType::CloseButtonHint | ElaAppBarType::ThemeChangeButtonHint);

    // 禁用调整大小
    setIsFixedSize(true);

    // 居中显示
    setParentCenter();

    setupUI();
    loadSettings();
}

void SettingsDialog::setParentCenter()
{
    if (parentWidget())
    {
        // 在父窗口中心显示
        QPoint center = parentWidget()->geometry().center();
        move(center.x() - width() / 2, center.y() - height() / 2);
    }
    else
    {
        // 没有父窗口则在屏幕中心显示
        moveToCenter();
    }
}

void SettingsDialog::setupUI()
{
    // 创建设置控件
    m_themeCombo = new QComboBox(this);
    m_themeCombo->addItem("浅色主题", static_cast<int>(ElaThemeType::Light));
    m_themeCombo->addItem("深色主题", static_cast<int>(ElaThemeType::Dark));
    m_themeCombo->addItem("跟随系统", static_cast<int>(ElaThemeType::Light));  // 简化示例

    m_opacitySpin = new QSpinBox(this);
    m_opacitySpin->setRange(30, 100);
    m_opacitySpin->setSuffix("%");
    m_opacitySpin->setValue(100);

    m_userNameEdit = new QLineEdit(this);
    m_userNameEdit->setPlaceholderText("请输入用户名");

    m_languageCombo = new QComboBox(this);
    m_languageCombo->addItems({"简体中文", "English", "日本語", "Français"});

    // 创建按钮
    QPushButton* saveButton = new QPushButton("保存", this);
    saveButton->setDefault(true);
    connect(saveButton, &QPushButton::clicked, this, &SettingsDialog::saveSettings);

    QPushButton* resetButton = new QPushButton("重置", this);
    connect(resetButton, &QPushButton::clicked, this, &SettingsDialog::resetSettings);

    QPushButton* cancelButton = new QPushButton("取消", this);
    connect(cancelButton, &QPushButton::clicked, this, &SettingsDialog::closeButtonClicked);

    // 应用设置分组
    QGroupBox* appGroup = new QGroupBox("应用程序设置", this);
    QFormLayout* appLayout = new QFormLayout(appGroup);
    appLayout->addRow("主题模式:", m_themeCombo);
    appLayout->addRow("窗口不透明度:", m_opacitySpin);
    appLayout->addRow("界面语言:", m_languageCombo);

    // 用户设置分组
    QGroupBox* userGroup = new QGroupBox("用户设置", this);
    QFormLayout* userLayout = new QFormLayout(userGroup);
    userLayout->addRow("用户名:", m_userNameEdit);

    // 按钮布局
    QHBoxLayout* buttonLayout = new QHBoxLayout();
    buttonLayout->addStretch();
    buttonLayout->addWidget(saveButton);
    buttonLayout->addWidget(resetButton);
    buttonLayout->addWidget(cancelButton);

    // 主布局
    QVBoxLayout* mainLayout = new QVBoxLayout(this);
    mainLayout->setContentsMargins(20, getAppBarHeight() + 20, 20, 20);
    mainLayout->setSpacing(15);
    mainLayout->addWidget(appGroup);
    mainLayout->addWidget(userGroup);
    mainLayout->addStretch();
    mainLayout->addLayout(buttonLayout);

    setLayout(mainLayout);
}

void SettingsDialog::loadSettings()
{
    // 从QSettings加载设置（简化示例）
    QSettings settings;

    QString userName = settings.value("user/name", "用户").toString();
    int themeIndex = settings.value("app/theme", 0).toInt();
    int opacity = settings.value("app/opacity", 100).toInt();
    int language = settings.value("app/language", 0).toInt();

    m_userNameEdit->setText(userName);
    m_themeCombo->setCurrentIndex(themeIndex);
    m_opacitySpin->setValue(opacity);
    m_languageCombo->setCurrentIndex(language);
}

void SettingsDialog::saveSettings()
{
    // 保存设置到QSettings
    QSettings settings;

    settings.setValue("user/name", m_userNameEdit->text());
    settings.setValue("app/theme", m_themeCombo->currentIndex());
    settings.setValue("app/opacity", m_opacitySpin->value());
    settings.setValue("app/language", m_languageCombo->currentIndex());

    // 应用主题设置
    ElaThemeType::ThemeMode themeMode = static_cast<ElaThemeType::ThemeMode>(m_themeCombo->currentData().toInt());
    eTheme->setThemeMode(themeMode);

    // 应用透明度设置（简化示例）
    if (parentWidget())
    {
        parentWidget()->setWindowOpacity(m_opacitySpin->value() / 100.0);
    }

    QMessageBox::information(this, "成功", "设置已保存！");
    close();
}

void SettingsDialog::resetSettings()
{
    QMessageBox::StandardButton result = QMessageBox::question(this, "确认重置", "确定要重置所有设置为默认值吗？", QMessageBox::Yes | QMessageBox::No, QMessageBox::No);

    if (result == QMessageBox::Yes)
    {
        m_userNameEdit->setText("用户");
        m_themeCombo->setCurrentIndex(0);
        m_opacitySpin->setValue(100);
        m_languageCombo->setCurrentIndex(0);

        QMessageBox::information(this, "重置完成", "设置已重置为默认值。");
    }
}